package cn.taobaoke.restful.util;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * 日期工具类。
 *
 * @author yangzuo
 * @since 2017-04-06
 */
public class DateUtil {

    public static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public LocalDateTime stringToDateTime(final String dateTimeString) {
        return LocalDateTime.parse(dateTimeString, formatter);
    }

    /**
     * 返回date1和date2之间的毫秒数.
     *
     * @param date1
     * @param date2
     * @return
     */
    public static long dateTimeDiff(final Date date1, final Date date2) {
        return date1.getTime() - date2.getTime();
    }

    /**
     * 将LocalDateTime转换称指定格式的字符串
     * @param dateTime
     * @return
     */
    public static String dateTimeToString(LocalDateTime dateTime) {
        return dateTime.format(formatter);
    }

    /**
     * 返回当前时间的格式字符串
     * @return
     */
    public static String currentDateTimeString() {
        return dateTimeToString(LocalDateTime.now());
    }
}
